package test;

import dao.impl.RecordDAOImpl;
import entity.Budget;
import entity.Expense;
import entity.Income;
import entity.Record;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;

public class PersonalFinanceManager {
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws SQLException {
        boolean running = true;
        while (running) {
            System.out.println("=================================\n欢迎使用个人账单管理系统\n=================================");
            System.out.println("请选择操作：");
            System.out.println("1. 记录收入");
            System.out.println("2. 记录支出");
            System.out.println("3. 查看所有账单");
            System.out.println("4. 查询账单");
            System.out.println("5. 设置月度预算");
            System.out.println("6. 查看月度统计报告");
            System.out.println("7. 退出系统");
            System.out.print("请输入选项序号：");

            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    recordIncome();
                    break;
                case 2:
                    recordExpense();
                    break;
                case 3:
                    displayAllRecords();
                    break;
                case 4:
                    queryRecords();
                    break;
                case 5:
                    setMonthlyBudget();
                    break;
                case 6:
                    displayMonthlyReport();
                    break;
                case 7:
                    running = false;
                    break;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
            System.out.println("按任意键返回主菜单...");
            scanner.nextLine();
        }
        scanner.close();
    }



    //记录收入
    private static void recordIncome() {
        System.out.println("请输入收入信息：");
        System.out.print("日期（YYYY-MM-DD）：");
        String date = scanner.nextLine();
        System.out.print("金额：");
        double amount = scanner.nextDouble();
        scanner.nextLine();
        System.out.print("类别（如工资、奖金等）：");
        String category = scanner.nextLine();
        System.out.print("备注：");
        String remark = scanner.nextLine();
        if (amount <= 0) {
            System.out.println("收入金额必须为正数！");
            return;
        }
        Income income = new Income(date, amount, category, remark);
        RecordDAOImpl record= new RecordDAOImpl();
        boolean result = record.addIncome(income);
        System.out.println("收入已成功记录！");
    }
    //记录支出
    private static void recordExpense() {
        System.out.println("请输入支出信息：");
        System.out.print("日期（YYYY-MM-DD）：");
        String date = scanner.nextLine();
        System.out.print("金额：");
        double amount = scanner.nextDouble();
        scanner.nextLine(); // Consume newline
        System.out.print("类别（如餐饮、交通、购物等）：");
        String category = scanner.nextLine();
        System.out.print("备注：");
        String remark = scanner.nextLine();

        if (amount <= 0) {
            System.out.println("支出金额必须为正数！");
            return;
        }
        Expense expense = new Expense(date, amount, category, remark);
        RecordDAOImpl record= new RecordDAOImpl();
        boolean result = record.addExpense(expense);
        System.out.println("支出已成功记录！");
    }
    //查看所有账单
    private static void displayAllRecords(){
        RecordDAOImpl record= new RecordDAOImpl();
        ArrayList<Record> records = null;
        try {
            records = record.displayAllRecord();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println("\n所有收入记录：");
        for (int i = 0; i < records.size(); i++){
            if (records.get(i).getAmount()>0){
                System.out.println(records.get(i).toString());
            }
        }
        System.out.println("\n所有支出记录：");
        for (int i = 0; i < records.size(); i++){
            if (records.get(i).getAmount()<0){
                System.out.println(records.get(i).toString());
            }
        }
    }

    private static void queryRecords() {
        System.out.print("请输入查询内容：");
        String keyword = scanner.next();
        RecordDAOImpl record = new RecordDAOImpl();
        ArrayList<Record> records = null;
        try {
            records = record.queryRecord(keyword);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println("\n查询结果：");
        for (int i = 0; i < records.size(); i++) {
            System.out.println(records.get(i).toString());
        }
    }

    private static void setMonthlyBudget() {
        System.out.println("请输入月度：");
        int month= scanner.nextInt();
        System.out.print("请输入预算：");
        double amount = scanner.nextDouble();
        Budget budget=new Budget(amount,month);
        RecordDAOImpl record = new RecordDAOImpl();
        record.setMonthlyBudget(budget);
        System.out.println("月度预算设置成功！");
    }
    private static void displayMonthlyReport() {
        RecordDAOImpl record= new RecordDAOImpl();
        ArrayList<Record> records = null;
        Double totalIncome = 0.0;
        Double totalExpense = 0.0;
        try {
            records = record.displayAllRecord();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        for (int i = 0; i < records.size(); i++){
            if (records.get(i).getAmount()>0){
                totalIncome += records.get(i).getAmount();
            }
        }
        System.out.println("总收入：" + totalIncome);
        for (int i = 0; i < records.size(); i++){
            if (records.get(i).getAmount()<0){
                totalExpense += records.get(i).getAmount();
            }
        }
        System.out.println("总支出：" + totalExpense);
    }
}